<?php
class categories
{
	var $ipsclass;
	
	var $order = array(
							'item' => 'item_id',
							'icon' => 'icon',
							'name' => 'item_name',
							'category' => '',
							'stock' => 'stock',
							'resell' => 'resell',
							'price' => 'price',
							'tax' => 'total_tax',
							'expire' => 'expire',
						);
	var $order_by = array(
							'ASC' => 1,
							'DESC' => -1,
						);
	var $order_default = array(
									'item' => 'ASC',
									'icon' => 'ASC',
									'name' => 'ASC',
									'category' => 'ASC',
									'stock' => 'DESC',
									'resell' => 'DESC',
									'price' => 'ASC',
									'tax' => 'ASC',
									'expire' => 'DESC',
								);
	
	function auto_run()
	{
		$this->ipsclass->input['category'] = intval($this->ipsclass->input['category']);
		$this->ipsclass->input['all_items'] = intval($this->ipsclass->input['all_items']);
		
		if($this->ipsclass->input['category'] != 0 and !is_array($this->ipsclass->cache['market_category'][$this->ipsclass->input['category']]))
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		// List sub cats
		if(!$this->ipsclass->input['category'])
		{
			$this->ipsclass->input['all_items'] = 1;
			$this->ipsclass->input['category'] = -1;
		}
		
		$html = $this->recursive_process_cat($this->ipsclass->input['category']);
		
		if(!isset($this->order[$this->ipsclass->input['order']]))
		{
			$this->ipsclass->input['order'] = 'stock';
		}
		if(!isset($this->order_by[$this->ipsclass->input['by']]))
		{
			$this->ipsclass->input['by'] = $this->order_default[$this->ipsclass->input['order']];
		}
		
		if($this->ipsclass->input['by'] == $this->order_default[$this->ipsclass->input['order']])
		{
			if($this->order_default[$this->ipsclass->input['order']] == 'ASC')
			{
				$this->order_default[$this->ipsclass->input['order']] = 'DESC';
			}
			else
			{
				$this->order_default[$this->ipsclass->input['order']] = 'ASC';
			}
		}
		
		uasort($html['items'],array(&$this,'order'));
		
		foreach($html['items'] as $i => $item_id)
		{
			if($this->ipsclass->cache['market_items'][$item_id]['hide_item'])
			{
				unset($html['items'][$i]);
				continue;
			}
			
			$this->ipsclass->cache['market_items'][$item_id]['fstock'] = $this->ipsclass->cache['market_items'][$item_id]['stock'] <= 0 ? ($this->ipsclass->cache['market_items'][$item_id]['stock'] == 0 ? "<span class='warnbad'>".$this->ipsclass->lang['none']."</span>" : $this->ipsclass->lang['infinite']) : $this->ipsclass->do_number_format($this->ipsclass->cache['market_items'][$item_id]['stock']);
			$this->ipsclass->cache['market_items'][$item_id]['fprice'] = $this->ipsclass->cache['market_items'][$item_id]['price'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($this->ipsclass->cache['market_items'][$item_id]['price']);
			$this->ipsclass->cache['market_items'][$item_id]['fresell'] = $this->ipsclass->cache['market_items'][$item_id]['resell'] <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($this->ipsclass->cache['market_items'][$item_id]['resell']);
			$this->ipsclass->cache['market_items'][$item_id]['ftotal_tax'] = $this->ipsclass->cache['market_items'][$item_id]['total_tax'] <= 0 ? $this->ipsclass->lang['no_tax'] : $this->ipsclass->numbers->do_number_format($this->ipsclass->cache['market_items'][$item_id]['total_tax'])."%";
			$this->ipsclass->cache['market_items'][$item_id]['frestock'] = $this->ipsclass->cache['market_items'][$item_id]['restock_time'] == 0 ? "<i>".$this->ipsclass->lang['never']."</i>" : ($this->ipsclass->cache['market_items'][$item_id]['restock_time'] <= time() ? "<span class='warngood'><u>".$this->ipsclass->lang['now']."</u></span>" : $this->ipsclass->get_date($this->ipsclass->cache['market_items'][$item_id]['restock_time'],'TINY'));
			$this->ipsclass->cache['market_items'][$item_id]['expiring'] = $this->ipsclass->cache['market_items'][$item_id]['expire_time'] == 0 ? "<i>".$this->ipsclass->lang['never']."</i>" : $this->ipsclass->numbers->time_string($this->ipsclass->cache['market_items'][$item_id]['expire'],'TINY');
			$this->ipsclass->cache['market_items'][$item_id]['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item_id]->parse_store_description()));
		}
		
		$this->ipsclass->input['category'] = $this->ipsclass->input['category'] == -1 ? 0 : $this->ipsclass->input['category'];
		$this->ipsclass->cache['market_category'][0]['cat_name'] = "<i>".$this->ipsclass->lang['all_categories']."</i>";
		
		$show = $this->ipsclass->market_class->toggle_show('market_sub_cat_'.$this->ipsclass->input['category']);
		
		$this->ipsclass->output = $this->ipsclass->compiled_templates['skin_market']->category_page($html,$this->order_default,$show);
		
		if($this->ipsclass->input['category'] > 0)
		{
			$temp = $this->ipsclass->input['category'];
			while($temp > 0)
			{
				array_unshift($this->ipsclass->nav,"<a href='".$this->ipsclass->market_class->base_url."CODE=shop&amp;category=".$temp."&amp;all_items=".$this->ipsclass->input['all_items']."&amp;order=".$this->ipsclass->input['order']."&amp;by=".$this->ipsclass->input['by']."'>".$this->ipsclass->cache['market_category'][$temp]['cat_name']."</a>");
				$temp = $this->ipsclass->cache['market_category'][$temp]['cat_parent_id'];
			}
		}
		array_push($this->ipsclass->nav,"<a href='".$this->ipsclass->market_class->base_url."CODE=shop&amp;order=".$this->ipsclass->input['order']."&amp;by=".$this->ipsclass->input['by']."'>".$this->ipsclass->lang['view_items']."</a>");
	}
	
	function recursive_process_cat($cat_id)
	{
		$info = array('subcats'=>array(),'items'=>array());
		
		$this->ipsclass->cache['market_category'][$cat_id]['count_subcat'] = 0; 
		$this->ipsclass->cache['market_category'][$cat_id]['count_items'] = 0; 
		$this->ipsclass->cache['market_category'][$cat_id]['count_stock'] = 0;
		$this->ipsclass->cache['market_category'][$cat_id]['total_subcat'] = 0; 
		$this->ipsclass->cache['market_category'][$cat_id]['total_items'] = 0; 
		$this->ipsclass->cache['market_category'][$cat_id]['total_stock'] = 0;
		
		if(is_array($this->ipsclass->market_class->category_tree[$cat_id]))
		{
			$this->ipsclass->cache['market_category'][$cat_id]['count_subcat'] = count($this->ipsclass->market_class->category_tree[$cat_id]);
			foreach(array_keys($this->ipsclass->market_class->category_tree[$cat_id]) as $sub_id)
			{
				$sub_info = $this->recursive_process_cat($sub_id);
				
				if($this->ipsclass->input['all_items'])
				{
					$info['items'] = array_merge($info['items'],$sub_info['items']);
				}
				
				$this->ipsclass->cache['market_category'][$cat_id]['total_items'] = $this->ipsclass->numbers->add($this->ipsclass->cache['market_category'][$cat_id]['total_items'],$this->ipsclass->cache['market_category'][$sub_id]['total_items']);
				if($this->ipsclass->cache['market_category'][$cat_id]['total_stock'] < 0 || $this->ipsclass->cache['market_category'][$sub_id]['total_stock'] < 0)
				{
					$this->ipsclass->cache['market_category'][$cat_id]['total_stock'] = -1;
				}
				else
				{
					$this->ipsclass->cache['market_category'][$cat_id]['total_stock'] = $this->ipsclass->numbers->add($this->ipsclass->cache['market_category'][$cat_id]['total_stock'],$this->ipsclass->cache['market_category'][$sub_id]['total_stock']);
				}
				
				$this->ipsclass->cache['market_category'][$sub_id]['total_subcat'] = $this->ipsclass->cache['market_category'][$sub_id]['total_subcat'] == 0 ? $this->ipsclass->lang['none'] : $this->ipsclass->do_number_format($this->ipsclass->cache['market_category'][$sub_id]['total_subcat']);
				$this->ipsclass->cache['market_category'][$sub_id]['count_subcat'] = $this->ipsclass->cache['market_category'][$sub_id]['count_subcat'] == 0 ? $this->ipsclass->lang['none'] : $this->ipsclass->do_number_format($this->ipsclass->cache['market_category'][$sub_id]['count_subcat']);
				$this->ipsclass->cache['market_category'][$sub_id]['total_items'] = $this->ipsclass->cache['market_category'][$sub_id]['total_items'] == 0 ? $this->ipsclass->lang['none'] : $this->ipsclass->do_number_format($this->ipsclass->cache['market_category'][$sub_id]['total_items']);
				$this->ipsclass->cache['market_category'][$sub_id]['count_items'] = $this->ipsclass->cache['market_category'][$sub_id]['count_items'] == 0 ? $this->ipsclass->lang['none'] : $this->ipsclass->do_number_format($this->ipsclass->cache['market_category'][$sub_id]['count_items']);
				$this->ipsclass->cache['market_category'][$sub_id]['total_stock'] = $this->ipsclass->cache['market_category'][$sub_id]['total_stock'] == 0 ? $this->ipsclass->lang['none'] : ($this->ipsclass->cache['market_category'][$sub_id]['total_stock'] < 0 ? $this->ipsclass->lang['infinite'] : $this->ipsclass->do_number_format($this->ipsclass->cache['market_category'][$sub_id]['total_stock']));
				$this->ipsclass->cache['market_category'][$sub_id]['count_stock'] = $this->ipsclass->cache['market_category'][$sub_id]['count_stock'] == 0 ? $this->ipsclass->lang['none'] : ($this->ipsclass->cache['market_category'][$sub_id]['count_stock'] < 0 ? $this->ipsclass->lang['infinite'] : $this->ipsclass->do_number_format($this->ipsclass->cache['market_category'][$sub_id]['count_stock']));
				$info['subcats'][] = $sub_id;
			}
		}
		
		$items = array();
		if(is_array($this->ipsclass->market_class->item_by_cat[$cat_id]))
		{
			foreach($this->ipsclass->market_class->item_by_cat[$cat_id] as $item_id => $item)
			{
				if($this->ipsclass->cache['market_items'][$item_id]['hide_item'])
				{
					continue;
				}
				
				$this->ipsclass->cache['market_category'][$cat_id]['count_items']++;
				if($this->ipsclass->cache['market_category'][$cat_id]['count_stock'] < 0 || $item['stock'] < 0)
				{
					$this->ipsclass->cache['market_category'][$cat_id]['count_stock'] = -1;
				}
				else
				{
					$this->ipsclass->cache['market_category'][$cat_id]['count_stock'] = $this->ipsclass->numbers->add($this->ipsclass->cache['market_category'][$cat_id]['count_stock'],$item['stock']);
				}
				
				if($this->ipsclass->input['all_items'] or $this->ipsclass->input['category'] == $cat_id)
				{
					$this->ipsclass->cache['market_items'][$item_id]['expire'] = $this->ipsclass->cache['market_items'][$item_id]['expire_time'] * $this->ipsclass->cache['market_items'][$item_id]['expire_unit'];
					$items[] = $item_id;
				}
			}
		}
		$info['items'] = array_merge($items,$info['items']);
		
		$this->ipsclass->cache['market_category'][$cat_id]['total_subcat'] = $this->ipsclass->numbers->add($this->ipsclass->cache['market_category'][$cat_id]['total_subcat'],$this->ipsclass->cache['market_category'][$cat_id]['count_subcat']);
		$this->ipsclass->cache['market_category'][$cat_id]['total_items'] = $this->ipsclass->numbers->add($this->ipsclass->cache['market_category'][$cat_id]['total_items'],$this->ipsclass->cache['market_category'][$cat_id]['count_items']);
		$this->ipsclass->cache['market_category'][$cat_id]['total_stock'] = $this->ipsclass->numbers->add($this->ipsclass->cache['market_category'][$cat_id]['total_stock'],$this->ipsclass->cache['market_category'][$cat_id]['count_stock']);
		if($this->ipsclass->cache['market_category'][$cat_id]['total_stock'] < 0 || $this->ipsclass->cache['market_category'][$cat_id]['count_stock'] < 0)
		{
			$this->ipsclass->cache['market_category'][$cat_id]['total_stock'] = -1;
		}
		
		return $info; 
	}
	
	function order(&$a,&$b)
	{
		if($this->order[$this->ipsclass->input['order']] != '')
		{
			$a_compare = $this->ipsclass->cache['market_items'][$a][$this->order[$this->ipsclass->input['order']]];
			$b_compare = $this->ipsclass->cache['market_items'][$b][$this->order[$this->ipsclass->input['order']]];
			if(is_numeric($a_compare) && is_numeric($b_compare))
			{
				$ab = $this->ipsclass->numbers->compare($a_compare,$b_compare);
				if($this->ipsclass->input['order'] == 'stock')
				{
					$_a = $this->ipsclass->numbers->compare($a_compare,0);
					$_b = $this->ipsclass->numbers->compare($b_compare,0);
					if(($ab > 0 || $_a < 0) && $_b >= 0)
					{
						return 1 * $this->order_by[$this->ipsclass->input['by']];
					}
					elseif(($ab < 0 || $_b < 0) && $_a >= 0)
					{
						return -1 * $this->order_by[$this->ipsclass->input['by']];
					}
					else
					{
						return $this->second_order($a,$b);
					}
				}
				if($this->ipsclass->input['order'] == 'expire')
				{
					if(($ab > 0 || $a_compare <= 0) && $b_compare > 0)
					{
						return 1 * $this->order_by[$this->ipsclass->input['by']];
					}
					elseif(($ab < 0 || $b_compare <= 0) && $a_compare > 0)
					{
						return -1 * $this->order_by[$this->ipsclass->input['by']];
					}
					else
					{
						return $this->second_order($a,$b);
					}
				}
				if($ab > 0)
				{
					return 1 * $this->order_by[$this->ipsclass->input['by']];
				}
				elseif($ab < 0)
				{
					return -1 * $this->order_by[$this->ipsclass->input['by']];
				}
				else
				{
					return $this->second_order($a,$b);
				}
			}
			else
			{
				$c = strcasecmp($a_compare,$b_compare);
				if($c != 0)
				{
					return $c * $this->order_by[$this->ipsclass->input['by']];
				}
				else
				{
					return $this->second_order($a,$b);
				}
			}
		}
		else
		{
			$a_2 = $this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$a]['category']];
			$b_2 = $this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$b]['category']];
			if($a_2 < $b_2)
			{
				return 1 * $this->order_by[$this->ipsclass->input['by']];
			}
			elseif($a_2 > $b_2)
			{
				return -1 * $this->order_by[$this->ipsclass->input['by']];
			}
			else
			{
				return $this->second_order($a,$b);
			}
		}
	}
	
	function second_order($a,$b)
	{
		$ab = $this->ipsclass->numbers->compare($this->ipsclass->cache['market_items'][$a]['stock'],$this->ipsclass->cache['market_items'][$b]['stock']);
		$_a = $this->ipsclass->numbers->compare($this->ipsclass->cache['market_items'][$a]['stock'],0);
		$_b = $this->ipsclass->numbers->compare($this->ipsclass->cache['market_items'][$b]['stock'],0);
		if(($ab > 0 || $_a < 0) && $_b >= 0)
		{
			return -1;
		}
		elseif($ab < 0 || $_b && $_a >= 0)
		{
			return 1;
		}
		$ab = $this->ipsclass->numbers->compare($this->ipsclass->cache['market_items'][$a]['price'],$this->ipsclass->cache['market_items'][$b]['price']);
		if($ab > 0)
		{
			return 1;
		}
		elseif($ab < 0)
		{
			return -1;
		}
		$ab = $this->ipsclass->numbers->compare($this->ipsclass->cache['market_items'][$a]['resell'],$this->ipsclass->cache['market_items'][$b]['resell']);
		if($ab < 0)
		{
			return 1;
		}
		elseif($ab > 0)
		{
			return -1;
		}
		$ab = $this->ipsclass->numbers->compare($this->ipsclass->cache['market_items'][$a]['total_tax'],$this->ipsclass->cache['market_items'][$b]['total_tax']);
		if($ab > 0)
		{
			return 1;
		}
		elseif($ab < 0)
		{
			return -1;
		}
		$c = strcasecmp($this->ipsclass->cache['market_items'][$a]['item_name'],$this->ipsclass->cache['market_items'][$b]['item_name']);
		if($c != 0)
		{
			return $c;
		}
		if($this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$a]['category']] < $this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$b]['category']])
		{
			return 1;
		}
		elseif($this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$a]['category']] > $this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$b]['category']])
		{
			return -1;
		}
		$c = strcasecmp($this->ipsclass->cache['market_items'][$a]['icon'],$this->ipsclass->cache['market_items'][$b]['icon']);
		if($c != 0)
		{
			return $c;
		}
		if($this->ipsclass->cache['market_items'][$a]['item_id'] < $this->ipsclass->cache['market_items'][$b]['item_id'])
		{
			return 1;
		}
		elseif($this->ipsclass->cache['market_items'][$a]['item_id'] > $this->ipsclass->cache['market_items'][$b]['item_id'])
		{
			return -1;
		}
		// Shuold never right here
		return 0;
	}
}
?>